package com.qianclass.talklive.app.im;
public class PacketHead {
	public int nPacketLen=0;
	public int nPacketID=0;
	public int nPacketType=0;
	public int nPayloadLen=0;
	public int nPayloadEncryp=0;
	public int nFromUid=0;
	public int nToid=0;
	public int nToAgid=0;
	
	public static final int headLen = 32;
	public byte[] head = new byte[headLen];
	
	
	public PacketHead(int nPacketLen,
			 int nPacketID,
			 int nPacketType,
			 int nPayloadLen,
			 int nPayloadEncryp,
			 int nFromUid,
			 int nToid,
			 int nToAgid)
	{
		System.arraycopy(Converter.int2byte(nPacketLen), 0, head, 0, 4);
		System.arraycopy(Converter.int2byte(nPacketID), 0, head, 4, 4);
		System.arraycopy(Converter.int2byte(nPacketType), 0, head, 8, 4);
		System.arraycopy(Converter.int2byte(nPayloadLen), 0, head, 12, 4);
		System.arraycopy(Converter.int2byte(nPayloadEncryp), 0, head, 16, 4);
		System.arraycopy(Converter.int2byte(nFromUid), 0, head, 20, 4);
		System.arraycopy(Converter.int2byte(nToid), 0, head, 24, 4);
		System.arraycopy(Converter.int2byte(nToAgid), 0, head, 28, 4);
	}
	
	public PacketHead(byte byteData[]) 
	{
	
		if(byteData.length<32)//������Ȳ���һ����ͷ
		{
			return;
		}
		
		// ����
		System.arraycopy(byteData, 0, head, 0, headLen);
		//������
		byte[] byteTemp = new byte[4];
		System.arraycopy(byteData,0,byteTemp,0,4);
		nPacketLen = Converter.byte2int(byteTemp);
		//��id
		System.arraycopy(byteData,4,byteTemp,0,4);
		nPacketID = Converter.byte2int(byteTemp);
		//������
		System.arraycopy(byteData,8,byteTemp,0,4);
		nPacketType = Converter.byte2int(byteTemp);
		//���ĳ���
		System.arraycopy(byteData,12,byteTemp,0,4);
		nPayloadLen = Converter.byte2int(byteTemp);
		//���ܷ���
		System.arraycopy(byteData,16,byteTemp,0,4);
		nPayloadEncryp = Converter.byte2int(byteTemp);
		//���ͷ�id
		System.arraycopy(byteData,20,byteTemp,0,4);
		nFromUid = Converter.byte2int(byteTemp);
		//���շ�id
		System.arraycopy(byteData,24,byteTemp,0,4);
		nToid = Converter.byte2int(byteTemp);
		//����ڵ�id
		System.arraycopy(byteData,28,byteTemp,0,4);
		nToAgid = Converter.byte2int(byteTemp);
		
	}
	
	static public byte[] MakePacketHead(int nPacketLen,
	 int nPacketID,
	 int nPacketType,
	 int nPayloadLen,
	 int nPayloadEncryp,
	 int nFromUid,
	 int nToid,
	 int nToAgid)
	{ 
		byte[] byteHead = new byte[32];
		System.arraycopy(Converter.int2byte(nPacketLen), 0, byteHead, 0, 4);
		System.arraycopy(Converter.int2byte(nPacketID), 0, byteHead, 4, 4);
		System.arraycopy(Converter.int2byte(nPacketType), 0, byteHead, 8, 4);
		System.arraycopy(Converter.int2byte(nPayloadLen), 0, byteHead, 12, 4);
		System.arraycopy(Converter.int2byte(nPayloadEncryp), 0, byteHead, 16, 4);
		System.arraycopy(Converter.int2byte(nFromUid), 0, byteHead, 20, 4);
		System.arraycopy(Converter.int2byte(nToid), 0, byteHead, 24, 4);
		System.arraycopy(Converter.int2byte(nToAgid), 0, byteHead, 28, 4);
		
		return byteHead;
	}
	
	public Integer GetFromId()
	{
		byte[] byteTemp = new byte[4];
		System.arraycopy(head,20,byteTemp,0,4);
		return Converter.byte2int(byteTemp);
	}
	
	public Integer GetToId()
	{
		byte[] byteTemp = new byte[4];
		System.arraycopy(head,24,byteTemp,0,4);
		return Converter.byte2int(byteTemp);
	}
	
	public Integer GetAgencyId()
	{
		byte[] byteTemp = new byte[4];
		System.arraycopy(head,28,byteTemp,0,4);
		return Converter.byte2int(byteTemp);
	}
}
